package com.ai.util;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ai.academiainteligente.*;

public class CategoryAdapter extends BaseAdapter {

	ArrayList<Category> categorias;

	public CategoryAdapter(ArrayList<Category> categorias) {
		this.categorias = categorias;
	}

	@Override
	public int getCount() {
		return categorias.size();
	}

	@Override
	public Object getItem(int arg0) {
		return categorias.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Category categoria = categorias.get(position);

		Context ctx = parent.getContext();

		ViewHolder holder;

		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.item_category, null);
			holder = new ViewHolder();
			holder.categoria = (TextView) convertView
					.findViewById(R.id.textView1);
			holder.imagemCategoria =(ImageView)convertView.findViewById(R.id.imageView1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Bitmap imagemCategoria = null;
		try {
			imagemCategoria = BitmapFactory.decodeStream(ctx.getAssets().open(
					categoria.getImagem()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		holder.categoria.setText(categoria.getDescricao());
		holder.imagemCategoria.setImageBitmap(imagemCategoria);

		return convertView;
	}

	static class ViewHolder {
		TextView categoria;
		ImageView imagemCategoria;
	}

}
